﻿using SalesSys.DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace SalesSys.BLL
{
    public interface IPriceConfirmDataBLL
    {
        /// <summary>
        /// 创建价格
        /// </summary>
        /// <param name="depotCommoditys">仓库商品</param> 
        /// <returns>批次编号</returns>
        Guid CreateBatch(DepotCommodity[] depotCommoditys, Employee emp);

        /// <summary>
        /// 根据批次查询价格审批
        /// </summary>
        /// <param name="BatchId">批次编号</param> 
        PriceConfirmData SelectFirst(Guid batchId);

    }
    public class PriceConfirmDataBLL : BaseBLL<PriceConfirmData, int>, IPriceConfirmDataBLL
    {
        static PriceConfirmDataBLL()
        {
            SequenceApprovalBLL.PriceConfirmApproval.ApprovalEndHandle += PriceConfirmApproval_ApprovalEndHandle;
        }
        /// <summary>
        /// 审批完成事件
        /// </summary>
        /// <param name="batchId">批次编号</param>
        /// <param name="success">成功失败</param>
        static void PriceConfirmApproval_ApprovalEndHandle(Guid batchId, bool success, string msg)
        {
            using (var tran = new TransactionScope())
            {
                PriceConfirmDataBLL bll = new PriceConfirmDataBLL();

                var priceConfirmDatas = bll.Rep.Entities.Where(p => p.BatchId == batchId).ToArray();
                foreach (var item in priceConfirmDatas)
                {
                    item.State = success ? ApprovalState.Agree : ApprovalState.Disagree;
                    bll.Edit(item);
                }
                EditDepotCommodityPrice(batchId);
                tran.Complete();
            }
        }
        /// <summary>
        /// 创建价格
        /// </summary>
        /// <param name="depotCommoditys">仓库商品</param> 
        /// <returns>批次编号</returns>
        public Guid CreateBatch(DepotCommodity[] depotCommoditys, Employee emp)
        {
            var batchId = BaseTypeEx.NewTimeGuid();
            using (var tran = new TransactionScope())
            {
                DepotCommodityBLL dcBLL = new DepotCommodityBLL();
                ProviderBLL pBLL = new ProviderBLL();
                CommodityBLL cBLL = new CommodityBLL();
                var dt = DateTime.Now;
                foreach (var item in depotCommoditys)
                {
                    var data = new PriceConfirmData();
                    var commodity = cBLL.Details(item.CommodityId);
                    data.Price = item.Price;
                    data.State = ApprovalState.Executing;
                    data.ProviderId = item.ProviderId;
                    data.ProviderName = pBLL.Details(item.ProviderId).Name;
                    data.BatchId = batchId;
                    data.CommodityId = item.CommodityId;
                    data.CommodityName = commodity.FillName();
                    data.CreaterId = emp.Id;
                    data.CreateTime = dt;
                    var old = dcBLL.Details(item.ProviderId, item.CommodityId);
                    data.OldPrice = old.Price;
                    data.SupplierName = commodity.Supplier.Name;
                    this.Rep.Insert(data);
                }
                tran.Complete();
            }
            return batchId;
        }

        /// <summary>
        /// 审批通过之后按批次进行仓库商品价格更新
        /// </summary>
        /// <param name="batchId">批次编号</param> BaseTypeEx.NewTimeGuid();
        /// <returns></returns>
        public static void EditDepotCommodityPrice(Guid batchId)
        {
            using (var tran = new TransactionScope())
            {
                DepotCommodityBLL dcBLL = new DepotCommodityBLL(); 
                PriceConfirmDataBLL bll = new PriceConfirmDataBLL();
                var priceConfirmDatas = bll.Rep.Entities.Where(p => p.BatchId == batchId).ToArray();
                foreach (var item in priceConfirmDatas)
                {
                    if (item.State == ApprovalState.Agree)
                    {
                        var old = dcBLL.Details(item.ProviderId, item.CommodityId);
                        old.Price = item.Price;
                        dcBLL.Edit(old);
                    }
                }
                tran.Complete();
            }
        }

        /// <summary>
        /// 根据批次查询价格审批
        /// </summary>
        /// <param name="BatchId">批次编号</param> 
        public PriceConfirmData SelectFirst(Guid batchId)
        {
            var priceConfirmDatas = this.Rep.Entities.Where(p => p.BatchId == batchId).FirstOrDefault();
            return priceConfirmDatas;
        }
    }
}
